package com.ev.hr.service.impl;

import com.ev.apis.model.DsResultResponse;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.framework.utils.StringUtils;
import com.ev.hr.domain.AdvertiseInfoDO;
import com.ev.hr.domain.EmployeeInfoDO;
import com.ev.hr.enums.EmployeeStateDict;
import com.ev.hr.service.AdvertiseInfoService;
import com.ev.hr.service.EmployeeInfoService;
import com.ev.hr.vo.PostInfoPageParam;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ev.hr.dao.PostInfoDao;
import com.ev.hr.domain.PostInfoDO;
import com.ev.hr.service.PostInfoService;
import com.ev.framework.utils.R;
import com.google.common.collect.Maps;


@Service
public class PostInfoServiceImpl implements PostInfoService {
	@Autowired
	private PostInfoDao postInfoDao;

	@Autowired
	private MessageSourceHandler messageSourceHandler;

	@Autowired
	private EmployeeInfoService employeeInfoService;

	@Autowired
	private AdvertiseInfoService advertiseInfoService;
	
	@Override
	public PostInfoDO get(Long id){
		return postInfoDao.get(id);
	}
	
	@Override
	public List<PostInfoDO> list(Map<String, Object> map){
		return postInfoDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return postInfoDao.count(map);
	}
	
	@Override
	public int save(PostInfoDO postInfo){
		return postInfoDao.save(postInfo);
	}
	
	@Override
	public int update(PostInfoDO postInfo){
		return postInfoDao.update(postInfo);
	}

	@Override
	public int updateAll(PostInfoDO postInfo){
		return postInfoDao.updateAll(postInfo);
	}
	
	@Override
	public int remove(Long id){
		return postInfoDao.remove(id);
	}
	
	@Override
	public R batchRemove(Long[] ids){
		List<String> useList = checkUse(ids);
		if(useList.size() > 0){
			return R.error(messageSourceHandler.getMessage("hr.post.used",new String[]{StringUtils.join(useList,",")}));
		}
		postInfoDao.batchRemove(ids);
		return R.ok();
	}

	@Override
	public R saveAndVerify(PostInfoDO saveParam){
		// 验证编号重复
		PostInfoDO checkDo = new PostInfoDO(saveParam.getId(),null, saveParam.getPostCode(),null);
		if(this.checkRepeat(checkDo)){
			return R.error(messageSourceHandler.getMessage("hr.post.duplicateCode",new String[]{saveParam.getPostCode()}));
		}
		//相同部门下面岗位名称不能重复
		checkDo = new PostInfoDO(saveParam.getId(),saveParam.getDeptId(),null,saveParam.getPostName());
		if(this.checkRepeat(checkDo)){
			return R.error(messageSourceHandler.getMessage("hr.post.duplicateJobName",new String[]{saveParam.getPostName()}));
		}
		if(saveParam.getId()!=null){
			return postInfoDao.update(saveParam)>0?R.ok():R.error();
		}
		return postInfoDao.save(saveParam)>0?R.ok():R.error();
	}

	@Override
	public Map<String, Object> getById(Long id) {
		PostInfoDO postInfo = postInfoDao.get(id);
		if (postInfo != null) {
			Map<String,Object> result = Maps.newHashMap();
			result.put("data",postInfo);
			//获取岗位在编人数
			List<Long> stateList = Lists.newArrayList();
			stateList.add(EmployeeStateDict.WORK.getId());
			stateList.add(EmployeeStateDict.PROBATION.getId());
			Map<String,Object> params = new HashMap<String,Object>(1){{put("employeePost",id);put("stateList",stateList);}};
			int count = employeeInfoService.count(params);
			result.put("alreadyCount",count);
			return R.ok(result);
		}
		return R.error();
	}

	@Override
	public Map<String, Object> pageList(PostInfoPageParam pageParam) {
		Map<String,Object> result = Maps.newHashMap();
		pageParam.setPostName(StringUtils.sqlLike(pageParam.getPostName()));
		List<Map<String, Object>> data = postInfoDao.pageList(pageParam);
		int total = postInfoDao.pageCount(pageParam);
		result.put("data", new DsResultResponse(pageParam.getPageno(),pageParam.getPagesize(),total,data));
		return R.ok(result);
	}

	private boolean checkRepeat(PostInfoDO postInfoDO) {
		return postInfoDao.checkRepeat(postInfoDO)>0;
	}

	private List<String> checkUse(Long[] ids){
		List<String> userString = new ArrayList<>();
		for(Long id : ids){
			PostInfoDO postInfoDO = get(id);
			//员工使用判断
			Map<String,Object> employeeInfoParam = new HashMap(){{put("employeePost",id);}};
			List<EmployeeInfoDO> employeeInfoDOList = employeeInfoService.list(employeeInfoParam);
			if(employeeInfoDOList.size()>0){
				userString.add(postInfoDO.getPostCode());
				continue;
			}
			//招聘需求使用判断
			Map<String,Object> advertiseParam = new HashMap(){{put("advertisePosition",id);}};
			List<AdvertiseInfoDO> advertiseInfoDOList = advertiseInfoService.list(advertiseParam);
			if(advertiseInfoDOList.size()>0){
				userString.add(postInfoDO.getPostCode());
				continue;
			}
		}
		return userString;
	}

}
